/*
 * FaceCat图形通讯框架(非开源)
 * 著作权编号:2015SR229355+2020SR0266727
 * 上海卷卷猫信息技术有限公司
 */

#include "stdafx.h"
#include "FCDateTimePicker.h"

namespace FaceCat{
    FCDateTimePicker::FCDateTimePicker(){
        m_calendar = 0;
        m_customFormat = L"yyyy-MM-dd";
        m_dropDownButton = 0;
        m_dropDownMenu = 0;
        m_showTime = true;
    }
    
    FCDateTimePicker::~FCDateTimePicker(){
        if(m_calendar){
            m_calendar->removeEvent((FCEventCallBack*)this, L"onselectedtimechanged");
            m_calendar = 0;
        }
        if(m_dropDownButton){
            m_dropDownButton->removeEvent((FCTouchEventCallBack*)this, L"ontouchdown");
            m_dropDownButton = 0;
        }
        if (m_dropDownMenu){
            getNative()->removeView(m_dropDownMenu);
            delete m_dropDownMenu;
            m_dropDownMenu = 0;
        }
    }
    
    FCCalendar* FCDateTimePicker::getCalendar(){
        return m_calendar;
    }
    
    String FCDateTimePicker::getCustomFormat(){
        return m_customFormat;
    }
    
    void FCDateTimePicker::setCustomFormat(const String& value){
        m_customFormat = value;
    }
    
    FCButton* FCDateTimePicker::getDropDownButton(){
        return m_dropDownButton;
    }
    
    FCMenu* FCDateTimePicker::getDropDownMenu(){
        return m_dropDownMenu;
    }
    
    bool FCDateTimePicker::showTime(){
        return m_showTime;
    }
    
    void FCDateTimePicker::setShowTime(bool value){
        m_showTime = value;
    }
    
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    void FCDateTimePicker::callEvent(String eventName, Object sender, Object invoke) {
        FCTextBox::callEvent(eventName, sender, invoke);
        if (eventName == L"onselectedtimechanged") {
            onSelectedTimeChanged();
        }
    }

    void FCDateTimePicker::callTouchEvent(String eventName, Object sender, FCTouchInfo touchInfo, Object invoke) {
        FCTextBox::callTouchEvent(eventName, sender, touchInfo, invoke);
        if (eventName == L"ontouchdown" && sender == m_dropDownButton) {
            onDropDownOpening();
        }
    }

    String FCDateTimePicker::getViewType(){
        return L"DateTimePicker";
    }
    
    void FCDateTimePicker::getAttribute(const String& name, String *value, String *type){
        if(name == L"customformat"){
            *type = L"text";
            *value = getCustomFormat();
        }
        else if(name == L"showtime"){
            *type = L"bool";
            *value = FCTran::boolToStr(showTime());
        }
        else{
            FCTextBox::getAttribute(name, value, type);
        }
    }
    
    ArrayList<String> FCDateTimePicker::getAttributeNames(){
        ArrayList<String> attributeNames = FCTextBox::getAttributeNames();
        attributeNames.add(L"CustomFormat");
        attributeNames.add(L"ShowTime");
        return attributeNames;
    }
    
    void FCDateTimePicker::onDropDownOpening(){
        m_calendar->setSize(m_dropDownMenu->getSize());
        if(m_calendar && !m_showTime){
            m_calendar->getTimeDiv()->setHeight(0);
        }
        m_dropDownMenu->setNative(getNative());
        FCPoint popupPoint ={0, getHeight()};
        FCPoint nativePoint = pointToNative(popupPoint);
        m_dropDownMenu->setLocation(nativePoint);
        m_dropDownMenu->setVisible(true);
        if (m_calendar){
            m_calendar->setMode(FCCalendarMode_Day);
        }
        m_dropDownMenu->bringToFront();
        m_dropDownMenu->invalidate();
    }
    
    void FCDateTimePicker::onLoad(){
        FCTextBox::onLoad();
        FCHost *host = getNative()->getHost();
        if (!m_dropDownButton){
            m_dropDownButton = dynamic_cast<FCButton*>(host->createInternalView(this, L"dropdownbutton"));
            addView(m_dropDownButton);
            m_dropDownButton->addEvent((FCTouchEventCallBack*)this, L"ontouchdown", this);
        }
        if (!m_dropDownMenu){
            m_dropDownMenu = dynamic_cast<FCMenu*>(host->createInternalView(this, L"dropdownmenu"));
            m_dropDownMenu->setShowHScrollBar(false);
            m_dropDownMenu->setShowVScrollBar(false);
            getNative()->addView(m_dropDownMenu);
            if(!m_calendar){
                m_calendar = new FCCalendar;
                m_dropDownMenu->addView(m_calendar);
                m_calendar->addEvent((FCEventCallBack*)this, L"onselectedtimechanged", this);
            }
        }
        if(m_dropDownMenu){
            m_dropDownMenu->setNative(getNative());
        }
    }
    
    void FCDateTimePicker::onSelectedTimeChanged(){
        callEvents(L"onvaluechanged");
        if(m_calendar){
            CDay *selectedDay = m_calendar->getSelectedDay();
            if(selectedDay){
                setText(FCTran::getFormatDate(m_customFormat, selectedDay->getYear(), selectedDay->getMonth(), selectedDay->getDay(),
                                            m_calendar->getTimeDiv()->getHour(), m_calendar->getTimeDiv()->getMinute(), m_calendar->getTimeDiv()->getSecond()));
                invalidate();
            }
        }
    }
    
    void FCDateTimePicker::setAttribute(const String& name, const String& value){
        if(name == L"customformat"){
            setCustomFormat(value);
        }
        else if(name == L"showtime"){
            setShowTime(FCTran::strToBool(value));
        }
        else{
            FCTextBox::setAttribute(name, value);
        }
    }
    
    void FCDateTimePicker::update(){
        if(m_native && !m_native->m_lockUpdate){
            int width = getWidth(), height = getHeight(), uBottom = 0;
            if (m_dropDownButton){
                int dWidth = m_dropDownButton->getWidth();
                FCPoint location ={width - dWidth, 0};
                m_dropDownButton->setLocation(location);
                FCSize size ={dWidth, height};
                m_dropDownButton->setSize(size);
                FCPadding padding(0, 0, dWidth, 0);
                setPadding(padding);
            }
        }
    }
}
